//
//  VoiceDetailViewController.m
//  SmileRead
//
//  Created by dllo on 15/8/25.
//  Copyright (c) 2015年 JIZ. All rights reserved.
//

#import "VoiceDetailViewController.h"
#import "VoiceLayout.h"
#import "VoiceMusicCollectionViewCell.h"
#import "VoiceMusicModel.h"
#import "VoiceSongCollectionViewCell.h"
#import "UMSocial.h"


@interface VoiceDetailViewController ()<UICollectionViewDataSource>

@property (nonatomic, retain) UICollectionView *collectView;

@property (nonatomic, retain) NSDictionary *dicOfMusic;

@property (nonatomic, retain) VoiceMusicModel *modelOfMusic;

@end

@implementation VoiceDetailViewController


- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.dicOfMusic = [NSDictionary dictionary];
    }
    return self;
}

- (void)dealloc
{
    [_dicOfMusic release];
    [_modelOfMusic release];
    [_collectView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self createCollectionView];
}

- (void)createCollectionView
{
    VoiceLayout *layout = [[VoiceLayout alloc] init];
    
    self.collectView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:layout];
    self.collectView.pagingEnabled = YES;
    self.collectView.backgroundColor = [UIColor whiteColor];
    self.collectView.dataSource = self;
    
    // 注册
    [self.collectView registerClass:[VoiceMusicCollectionViewCell class] forCellWithReuseIdentifier:@"MusicCell"];
    [self.collectView registerClass:[VoiceSongCollectionViewCell class] forCellWithReuseIdentifier:@"SongCell"];
    
    [self.view addSubview:self.collectView];
}

- (void)receiveWithDic:(NSDictionary *)dic
{
    self.dicOfMusic = dic;
}

// 由于接受歌曲的model
- (void)receiveWithModel:(VoiceMusicModel *)model
{
    self.modelOfMusic = model;
    [self.collectView reloadData];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 2;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.item == 0) {
        VoiceMusicCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MusicCell" forIndexPath:indexPath];
        cell.backgroundColor = [UIColor whiteColor];
        cell.model = self.modelOfMusic;
        cell.targetOfback = self;
        cell.actionOfback = @selector(backClick:);
        
        // 触发分享
        cell.targetOfshare = self;
        cell.actionOfshare = @selector(share:);
        
        return cell;
    }
    
    else {
        if (self.modelOfMusic.playInfo == nil) {
            self.collectView.contentSize = CGSizeMake(self.view.frame.size.width, 0);
        }
        VoiceSongCollectionViewCell *cellOfSong = [collectionView dequeueReusableCellWithReuseIdentifier:@"SongCell" forIndexPath:indexPath];
        cellOfSong.model = self.modelOfMusic;
        return cellOfSong;
    }
}

// 分享
- (void)share:(UIImageView *)imageView
{
    UIImage *image = imageView.image;
    [UMSocialSnsService presentSnsIconSheetView:self appKey:@"55df0fa2e0f55aa742001bc4" shareText:@"来一句吧" shareImage:image shareToSnsNames:@[UMShareToRenren, UMShareToDouban, UMShareToSina, UMShareToTencent] delegate:nil];
}


// 点击返回
- (void)backClick:(UIButton *)button
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
